import urllib, urllib2
from django.http import HttpResponse, HttpResponseRedirect
import django.utils.simplejson as jsondata
from conf.BookmarkConf import BookmarkConf
import sys
class BookmarkData:

    def processUrl(self,url):
        try:
            f = urllib2.urlopen(url)
            json = f.read()    
        
            import re    
            new_str = re.sub('[$]', '', json)
            
            #return HttpResponse('Video title: %s' % new_str)
            json = jsondata.JSONDecoder().decode(new_str) 
            #return json
        except:
            new_str = '[{"d":"Looks like Delicious is down. Thanks for visiting us. Please try again latter."},{"u":"#null"}]'
            #return sys.exc_info()[1]
            json = jsondata.JSONDecoder().decode(new_str)
            raise         
        return json

    def getUserName(self):
        conf = BookmarkConf()
        userName = conf.getusername()
        return userName
    
    def getUserTags(self):    
        url = 'http://feeds.delicious.com/v2/json/tags/'+self.getUserName()+'?count=25'
        json = self.processUrl(url)
        return json

    def getBookMarks(self):    
        url = 'http://feeds.delicious.com/v2/json/'+self.getUserName()+'?count=10'
        json = self.processUrl(url)
        return json
    
    def getBookMarksByTag(self,tagName):    
        url = 'http://feeds.delicious.com/v2/json/'+self.getUserName()+'/'+tagName+'?count=10'
        json = self.processUrl(url)
        return json   